// 版权归 瞄你个汪 所有。使用本代码应遵守相关法律法规和Apache 2.0开源许可要求。
package top.geeke.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.geeke.config.cache.RedisCache;
import top.geeke.controller.dto.SysCacheKeyInput;
import top.geeke.controller.dto.SysCacheOutput;
import top.geeke.toolkit.http.Result;

import java.util.Set;

/**
 * <p>
 * 系统缓存管理 控制层
 * </p>
 * @author 瞄你个汪
 * @since 2024/6/6 10:37
 */
@Api(tags = "⚙ 系统缓存管理")
@RestController()
@RequestMapping("/api/sysCache")
public class SysCacheController {
    @Autowired
    private RedisCache redisCache;

    /**
     * <p>
     *  获取所有缓存键
     * </p>
     */
    @PostMapping("getKeys")
    @ApiOperation(value = "获取所有键", notes = "获取所有缓存键")
    public Result<Set<String>> getKeys() {
        return Result.ok(redisCache.getKeys());
    }

    /**
     * <p>
     *  获取指定缓存值
     * </p>
     */
    @PostMapping("get")
    @ApiOperation(value = "获取缓存", notes = "获取所有缓存键")
    public Result<SysCacheOutput> get(@Validated @RequestBody SysCacheKeyInput input) {
        Object value = redisCache.get(input.getKey());
        if (value == null) return Result.ok();
        Long expire = redisCache.getExpire(input.getKey());
        return Result.ok(new SysCacheOutput(value, expire));
    }

    /**
     * <p>
     *  删除指定缓存
     * </p>
     */
    @PostMapping("remove")
    @ApiOperation(value = "删除缓存", notes = "删除指定缓存")
    public Result<Boolean> remove(@Validated @RequestBody SysCacheKeyInput input) {
        return Result.ok(redisCache.remove(input.getKey()));
    }

    /**
     * <p>
     *  清空所有缓存
     * </p>
     */
    @PostMapping("clear")
    @ApiOperation(value = "清空缓存", notes = "清空所有缓存")
    public Result<Long> clear() {
        return Result.ok(redisCache.clear());
    }
}
